basket.require({
	url : 'http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js'
}, {
	url : "http://cdn.bootcss.com/angular.js/1.4.3/angular.min.js"
}, {
	url : "http://cdn.bootcss.com/alertify.js/0.3.11/alertify.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/spark-md5.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/persistence.js"
}).then(ready);

function ready() {
	persistence.setNamespace("peiwan_admin");
	var peiwanAdmin = angular.module('peiwan_admin', []);
	peiwanAdmin.controller('PromoterListCtrl', [ "$scope", function(s) {
		var offset = 0;
		var LIMIT = 5;
		if (!persistence.get("session")) {
			location.href = "admin_login.html";
			return;
		}
		var queryList = function() {
			ajax("/findPromoters", {
				session : persistence.get("session"),
				offset : offset,
				limit : LIMIT
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				s.$apply(function() {
					s.promoters = json.entity;
				});
			});
		}
		queryList();
		s.prePage = function() {
			if (offset >= LIMIT) {
				offset -= LIMIT;
				var page = parseInt($("#page_input").val());
				$("#page_input").val(--page + "");
				queryList();
			}

		}
		s.toPage = function() {
			var page = parseInt($("#page_input").val());
			offset = (page - 1) * LIMIT;
			queryList();
		}
		s.nextPage = function() {
			offset += LIMIT;
			var page = parseInt($("#page_input").val());
			$("#page_input").val(++page + "");
			queryList();
		}
		s.showDistricts = function(id, name) {
			document.getElementById("district_list_dialog").showModal();
			s.promoterName = name;
			s.curPromoterId = id;
			ajax("/findDistrictRelationsByPromoterId", {
				session : persistence.get("session"),
				promoterId : s.curPromoterId
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				s.$apply(function() {
					s.districtRelations = json.entity;
				});
			});
		}
		s.deletePromoter = function(id) {
			if (confirm("确认删除？")) {
				ajax("/deletePromoter", {
					session : persistence.get("session"),
					id : id
				}, function(data) {
					var json = JSON.parse(data);
					if (json.status == 1) {
						location.href = "admin_login.html";
						return;
					}
					queryList();
				});
			}
		}
		s.insertOk = function() {
			var username = $("#insert_username").val();
			var password = $("#insert_password").val();
			var name = $("#insert_name").val();
			var alipayUsername = $("#insert_alipay_username").val();
			var rate = $("#insert_rate").val();
			password = SparkMD5.hash(password);
			ajax("/insertPromoter", {
				session : persistence.get("session"),
				username : username,
				password : password,
				name : name,
				alipayUsername : alipayUsername,
				rate : rate
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				} else if (json.status == 1) {
					alert("重复的推广人账号");
					return;
				}
				queryList();
			});
			document.getElementById("insert_promoter_dialog").close();
		}
		s.insertCancel = function() {
			document.getElementById("insert_promoter_dialog").close();
		}
		s.createPromoter = function() {
			document.getElementById("insert_promoter_dialog").showModal();
		}
		function queryPromoterDistricts() {
			ajax("/findDistrictRelationsByPromoterId", {
				session : persistence.get("session"),
				promoterId : s.curPromoterId
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				s.$apply(function() {
					s.districtRelations = json.entity;
				});
			});
		}
		s.insertDistrict = function() {
			var city = $("#district_insert_city").val();
			var district = $("#district_insert_district").val();
			ajax("/insertPromoterDistrictRelation", {
				session : persistence.get("session"),
				promoterId : s.curPromoterId,
				city : city,
				district : district
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				if (json.status == 2) {
					alert('市区已经被占用');
					return;
				}
				queryList();
				queryPromoterDistricts()
			});
			document.getElementById('insert_district_dialog').close();
		}
		s.deleteDistrict = function(id) {
			ajax("/deletePromoterDistrict", {
				session : persistence.get("session"),
				id : id
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				queryList();
				queryPromoterDistricts();
			});
		}
	} ]);
}